<?php


namespace apps\marriage\core\logic;


use think\Exception;
use think\facade\Log;
use vm\org\Mail;

class MailLogic
{
    private $mailConfig;
    /**
     * @var Mail
     */
    private $mailer = null;

    public function __construct()
    {
        $siteConfigService = service('SiteConfig', SERVICE_NAMESPACE);
        $siteConfig = $siteConfigService->getAllConfigs();
        $mailConfig = [
            'SMTP_HOST' => $siteConfig['stmp_host']['value'],
            'SMTP_PORT' => $siteConfig['stmp_port']['value'],
            'SMTP_USER' => $siteConfig['stmp_account']['value'],
            'SMTP_PASS' => $siteConfig['stmp_password']['value'],
            'FROM_EMAIL' => $siteConfig['stmp_from_mail']['value'],
            'FROM_NAME' => $siteConfig['stmp_from_name']['value'],
            'REPLY_EMAIL' => $siteConfig['stmp_reply_email']['value'],
            'REPLY_NAME' => $siteConfig['stmp_reply_name']['value'],
        ];
        $this->mailer = new Mail($mailConfig);
    }

    /**
     * 系统邮件发送函数
     *
     * @param string $to            接收邮件者邮箱
     * @param string $toName          接收邮件者名称
     * @param string $subject       邮件主题
     * @param string $body          邮件内容
     * @param string $attachment    附件列表
     * @throws
     * @return boolean
     */
    function send($to, $toName = '', $subject = '', $body = '', $attachment = null)
    {
        try {
            $result = $this->mailer->send($to, $toName, $subject, $body, $attachment);
        } catch (Exception $e) {
            Log::error($e->getTraceAsString());
            throw new Exception(lang('verify_code_send_failed'));
        }
    }
}